package cn.dgut.training.Scheduled;

import cn.dgut.training.controller.GoodsService;
import cn.dgut.training.emun.OrderStatusEnum;
import cn.dgut.training.entity.goods.Goods;
import cn.dgut.training.entity.goods.GoodsProduct;
import cn.dgut.training.entity.transaction.OrderConfig;
import cn.dgut.training.entity.transaction.OrderGoods;
import cn.dgut.training.entity.transaction.OrderStatusTime;
import cn.dgut.training.entity.transaction.Orders;
import cn.dgut.training.mapper.OrderConfigMapper;
import cn.dgut.training.mapper.OrderGoodsMapper;
import cn.dgut.training.mapper.OrderMapper;
import cn.dgut.training.mapper.OrderStatusTimeMapper;
import cn.hutool.core.date.DateTime;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrdersScheduled {

    @Resource
    private OrderConfigMapper orderConfigMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderGoodsMapper orderGoodsMapper;

    @Resource
    private OrderStatusTimeMapper orderStatusTimeMapper;

    @Resource
    private GoodsService goodsService;


    /**
     * 通过定时任务系统自动取消超时订单
     */
    @Scheduled(cron = "0 * * * * ? ")
    public void delOverdueCoupon()  {
        OrderConfig orderConfig=orderConfigMapper.selectById(1);
        Date date = DateTime.now();
        Date time = new Date(date.getTime()-Integer.valueOf(orderConfig.getOrderUnpaid())*60*1000);
        QueryWrapper<Orders> orderQueryWrapper=new QueryWrapper<>();
        orderQueryWrapper.lt("add_time",time).eq("order_status", OrderStatusEnum.WAITING_FOR_PAYMENT.getValue());
        List<Orders> orderList=orderMapper.selectList(orderQueryWrapper);
        if (orderList != null) {
            orderList.stream()
                    .map(orders -> {
                        //将订单状态设置为系统取消
                        orders.setOrderStatus(OrderStatusEnum.CANCELLATION_OF_SYSTEM.getValue());
                        orderStatusTimeMapper.insert(new OrderStatusTime().setOrderId(orders.getId())
                                .setOrderSn(orders.getOrderSn()).setOrderStatus(OrderStatusEnum.CANCELLATION_OF_SYSTEM.getValue()));
                        orderMapper.updateById(orders);
                        //将商品库存回调
                        List<OrderGoods> orderGoodsList=orderGoodsMapper.selectList(new QueryWrapper<OrderGoods>().lambda()
                                .eq(OrderGoods::getOrderId,orders.getId()));
                        for(OrderGoods orderGoods:orderGoodsList){
                            goodsService.updateStock(orderGoods.getProductId(),orderGoods.getNumber());
                        }
                        return null;
                    }).forEach(s -> {
            });
        }
    }

    /**
     * 通过定时任务系统自动收货
     */
    @Scheduled(cron = "0 * * * * ? ")
    public void ReceivedBySys(){
        OrderConfig orderConfig=orderConfigMapper.selectById(1);
        Date date = DateTime.now();
        Date time = new Date(date.getTime()-Integer.valueOf(orderConfig.getOrderUnconfirm())*24*60*60*1000);
        QueryWrapper<Orders> orderQueryWrapper=new QueryWrapper<>();
        orderQueryWrapper.lt("add_time",time).eq("order_status", OrderStatusEnum.WAITING_FOR_RECEIVED.getValue());
        List<Orders> orderList=orderMapper.selectList(orderQueryWrapper);
        if (orderList != null) {
            orderList.stream()
                    .map(orders -> {
                        //将订单状态设置为用户收货
                        orders.setOrderStatus(OrderStatusEnum.WAITING_FOR_EVALUATION.getValue());
                        orderStatusTimeMapper.insert(new OrderStatusTime().setOrderId(orders.getId())
                                .setOrderSn(orders.getOrderSn()).setOrderStatus(OrderStatusEnum.WAITING_FOR_EVALUATION.getValue()));
                        orderMapper.updateById(orders);
                        return null;
                    }).forEach(s -> {
            });
        }
    }

    /**
     * 通过定时任务系统取消评论资格
     */
    @Scheduled(cron = "0 * * * * ? ")
    public void CommentBySys(){
        OrderConfig orderConfig=orderConfigMapper.selectById(1);
        Date date = DateTime.now();
        Date time = new Date(date.getTime()-Integer.valueOf(orderConfig.getOrderComment())*24*60*60*1000);
        QueryWrapper<Orders> orderQueryWrapper=new QueryWrapper<>();
        orderQueryWrapper.lt("add_time",time).eq("order_status", OrderStatusEnum.WAITING_FOR_EVALUATION.getValue());
        List<Orders> orderList=orderMapper.selectList(orderQueryWrapper);
        if (orderList != null) {
            orderList.stream()
                    .map(orders -> {
                        //将订单状态设置为完成交易
                        orders.setOrderStatus(OrderStatusEnum.SUCCESSFUL_TRADE.getValue());
                        orders.setEndTime(DateTime.now());
                        List<OrderGoods> goodsList=orderGoodsMapper.selectList(new QueryWrapper<OrderGoods>()
                                .lambda()
                                .eq(OrderGoods::getOrderId,orders.getId()));
                        for(OrderGoods orderGoods:goodsList){
                            orderGoods.setComment(-1);
                            orderGoodsMapper.updateById(orderGoods);
                        }
                        orderStatusTimeMapper.insert(new OrderStatusTime().setOrderId(orders.getId())
                                .setOrderSn(orders.getOrderSn()).setOrderStatus(OrderStatusEnum.SUCCESSFUL_TRADE.getValue()));
                        orderMapper.updateById(orders);
                        return null;
                    }).forEach(s -> {
            });
        }
    }
}
